package com.ruoyi.dataserver.service.impl;

import com.ruoyi.dataserver.model.REDLINE;
import com.ruoyi.dataserver.service.IRedLineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * @ProjectName
 * @Package com.lab.dataserver.service.impl
 * @ClassName GhAnalyzeServceImpl
 * @Description 规划分析逻辑层
 * @Company lab
 * @Author wangzhx
 * @Date 2022/3/11  11:55
 * @UpdateUser
 * @UpdateDate
 * @UpdateRemark
 * @Version 1.0.0
 */
@Slf4j
@Component
public class RedLineServceImpl implements IRedLineService {

    @Autowired
    public JdbcTemplate jdbcTemplate;


    @Override
    public List<REDLINE> search() {
        RowMapper<REDLINE> rowMapper  = new BeanPropertyRowMapper<REDLINE>(REDLINE.class);
        String sqlLine = "select * from REDLINE  where 1 = 1 order by id desc";
        List<REDLINE> lineList = jdbcTemplate.query(sqlLine,rowMapper);
        return lineList;
    }

    @Override
    public String add(REDLINE dto) {
        try{
            RowMapper<REDLINE> rowMapper  = new BeanPropertyRowMapper<REDLINE>(REDLINE.class);
            String sqlSearch = "select * from REDLINE where name = '"+dto.getName()+"'";
            List<REDLINE> lineList = jdbcTemplate.query(sqlSearch,rowMapper);
            if(lineList.size() > 0){
                return "repeat";
            }else{
                String sql = "INSERT INTO REDLINE (name, geo) VALUES " +
                        "('"+dto.getName()+"'," +
                        "'"+dto.getGeo()+"'" +
                        ")";
                jdbcTemplate.execute(sql);
                return "success";
            }
        } catch (Exception e) {
            return "fail";
        }
    }



    @Override
    public String delete(String id) {
        try {
            String sql = "delete from REDLINE where id = '"+id+"'";
            jdbcTemplate.execute(sql);
            return "success";
        }
        catch (Exception e) {
            return "fail";
        }
    }
}
